package com.roomio.core.security.service.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.openid.OpenIDAttribute;
import org.springframework.security.openid.ext.OpenIdAuthenticationHandler;

import com.roomio.core.security.model.User;
import com.roomio.core.security.model.UserNotRegisteredException;

/** This handler used to process attributes, received from openId provider and create new user
 * 
 * @author Shailendra
 *
 */
public class AppAuthenticationSuccessHandler implements OpenIdAuthenticationHandler {
	
	private DatabaseUserDetailsService databaseUserDetailsService;
		
	public void setDatabaseUserDetailsService(
			DatabaseUserDetailsService databaseUserDetailsService) {
		this.databaseUserDetailsService = databaseUserDetailsService;
	}



	public void onOpenIdAuthenticationSuccess(HttpServletRequest request,
											  String identityUrl, 
											  List<OpenIDAttribute> attributes) {
		String email = null;
		String firstName = null;
		String lastName = null;
		String fullName = null;
		
		//try to get required params from attributes
		for (OpenIDAttribute attr : attributes) {
			if ("email".equals(attr.getName())) {
				email = (attr.getValues() != null && attr.getValues().size() > 0 ) ? attr.getValues().get(0) : null;
			} else if ("first".equals(attr.getName())) {
				firstName = (attr.getValues() != null && attr.getValues().size() > 0 ) ? attr.getValues().get(0) : null;
			} else if ("last".equals(attr.getName())) {
				lastName = (attr.getValues() != null && attr.getValues().size() > 0 ) ? attr.getValues().get(0) : null;
			} else if ("fullname".equals(attr.getName())) {
				fullName = (attr.getValues() != null && attr.getValues().size() > 0 ) ? attr.getValues().get(0) : null;
			}
		}
		
		if (StringUtils.isEmpty(fullName) &&
				(!StringUtils.isEmpty(firstName) || !StringUtils.isEmpty(lastName))) {
			fullName = firstName + " " + lastName;
		}
		
		if(email== null){
			throw new UsernameNotFoundException("Cannot find user " + identityUrl);
		}
		// add user with specified openId - if not exist
		try {
			databaseUserDetailsService.loadUserByUsername(email);
		}catch (UserNotRegisteredException ex) {
			// user not found - create new
			User user = ex.getUser();
			//user.setFullName(fullName);
			user.setFirstName(firstName);
			user.setLastName(lastName);
			user.setIdentityUrl(identityUrl);
			//user.setCreatedDate(new Date());
			user.setModifiedDate(new Date());
			user.setEnabled(true);
			user.setLoggedIn(true);
			databaseUserDetailsService.updateUser(user);
		} catch (UsernameNotFoundException ex) {
			// user not found - create new
			User user = new User();
			user.setEmail(email);
			user.setFullName(fullName);
			user.setIdentityUrl(identityUrl);
			user.setCreatedDate(new Date());
			user.setModifiedDate(new Date());
			user.setEnabled(true);
			user.setLoggedIn(true);
			databaseUserDetailsService.addUser(user);
		}
		System.out.println("sddsaf");
	}
}
